﻿Imports System.Data.SqlClient
Public Class Frm_sala7iyat
    Public Sub remplirClient()
        Partager.moi.remplirCompteEmplo()

        Dim htClient As New Hashtable()
        Dim str As String = ""
        For Each cmp As Compte In Partager.moi.ListCompte
            str = cmp.nom
            htClient.Add(cmp.Id, str)
        Next

        For Each ImageType As DictionaryEntry In htClient
            cmbLogin.Items.Add(ImageType)
        Next
        cmbLogin.DisplayMember = "value"
        cmbLogin.ValueMember = "key"
        If cmbLogin.Items.Count > 0 Then
            cmbLogin.SelectedIndex = 0
        End If
    End Sub

    Public Sub AffichermesList()
        Dim srt As String
        Dim nomForm As String
        Partager.utilist.remplirLstcmptFrm() 'tarik
        ListView1.Items.Clear()
        If Not Partager.utilist.listForm Is Nothing Then 'tarik
            Partager.remplirListForm(Partager.lstForm)
            For Each lst As ListViewItem In lstVForm.Items
                lst.Selected = False
            Next
            For Each frm In Partager.utilist.listForm 'tarik
                '/////////// ils séléctionne les form dont l'utilisateur y a des privilèges
                For Each lst As ListViewItem In lstVForm.Items
                    If frm.Id_Form = lst.SubItems(0).Text Then
                        lst.Selected = True
                        Exit For
                    End If
                Next
                If frm.isRead = True Then
                    srt = "للقراة فقط"
                Else
                    srt = "القراءة و الكتابة"
                End If


                Dim req As IEnumerable(Of String) = From frmReq In Partager.lstForm
                                                        Where frmReq.Id = frm.Id_Form
                                                        Select frmReq.legende

                nomForm = req(0)
                ListView1.Items.Add(New ListViewItem(New String() {frm.Id_Form, nomForm, srt}))
            Next
        End If

    End Sub
    Private Sub Frm_sala7iyat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Partager.remplirLstView(lstVForm)
        remplirClient()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Partager.close()
            If (lstVForm.SelectedIndices.Count > 0) Then
                If MsgBox("هل تريد فعلا إضافة الصلاحيات؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة") = MsgBoxResult.Yes Then

                    Partager.deClt = DirectCast(cmbLogin.SelectedItem, DictionaryEntry)
                    Partager.cmpform = New CompteFrom()

                    With Partager.cmpform
                        .Id_Compte = Partager.deClt.Key
                        For i As Integer = 0 To lstVForm.SelectedIndices.Count - 1
                            .Id_Form = CInt(lstVForm.SelectedItems(i).SubItems(0).Text)
                            .isRead = CheckBox1.Checked
                            Partager.moi.Ajouter_droit(Partager.cmpform)
                        Next
                    End With
                    Partager.moi.remplirLstcmptFrm()

                    MsgBox("تمت إضافة الصلاحيات للمستخدم", MsgBoxStyle.Information, "نجاح")
                    Partager.deselectViewRows(lstVForm)  'tarik
                    'Partager.vider(Me)
                    AffichermesList()
                End If
            Else
                MsgBox("اختر النافذة من فضلك", MsgBoxStyle.Critical, "! خـطأ")
            End If
        Catch sqlEx As SqlException
            If sqlEx.Message.Contains("PK_Compte_Droit") Then
                MsgBox("عفوا، هذه الصلاحية من قبل", MsgBoxStyle.Critical, "خطأ")
            Else
                MsgBox(sqlEx.Message)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub cmbLogin_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbLogin.SelectedIndexChanged
        ListView1.Items.Clear()
        Partager.deClt = DirectCast(cmbLogin.SelectedItem, DictionaryEntry)
        Dim idClt As Integer = CInt(Partager.deClt.Key)
        For Each uti As Compte In Partager.moi.ListCompte
            If idClt = uti.Id Then
                Partager.utilist = uti
                Exit For
            End If
        Next uti
        AffichermesList()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If ListView1.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذه الواجهة ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف واجهة") = MsgBoxResult.Yes Then
                Partager.cmpform = New CompteFrom
                Partager.deClt = DirectCast(cmbLogin.SelectedItem, DictionaryEntry)
                Dim idClt As Integer = CInt(Partager.deClt.Key)
                With Partager.cmpform
                    .Id_Form = CInt(ListView1.SelectedItems(0).SubItems(0).Text)
                    .Id_Compte = idClt
                End With
                Partager.moi.suprimmercmptFrm(Partager.cmpform.Id_Form)
                AffichermesList()
            End If
        Else
            MsgBox("اختر نافذة من فضلك", MsgBoxStyle.Critical, "خطأ")
        End If
    End Sub
End Class